package com.ma.implatform.filter;

import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;

public class CacheHttpServletRequestWrapper extends HttpServletRequestWrapper {
    private byte[] requestBody;
    private final HttpServletRequest request;
    public CacheHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
        this.request = request;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
         if(null ==this.requestBody){
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             IOUtils.copy(request.getInputStream(), baos);
             this.requestBody = baos.toByteArray();
         }
        ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);
         return new ServletInputStream() {
             @Override
             public boolean isFinished() {
                 return false;
             }

             @Override
             public boolean isReady() {
                 return false;
             }

             @Override
             public void setReadListener(ReadListener readListener) {

             }

             @Override
             public int read() throws IOException {
                 return bais.read();
             }
         };
    }
    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }

}
